package com.ctz.commonframe.utils;

import android.text.TextUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;

public class DateUtil {
	
	/**
	 * The default time zone in China.
	 */
	public static final String DEFAULT_TIME_ZONE = "GMT+8";
	
	/**
	 * Milliseconds in a standard second.
	 */
	public static final long MILLIS_PER_SECOND = 1000;
	
	/**
	 * Milliseconds in a standard minute.
	 */
	public static final long MILLIS_PER_MINUTE = 60 * MILLIS_PER_SECOND;
	
	/**
	 * Milliseconds in a standard hour.
	 */
	public static final long MILLIS_PER_HOUR = 60 * MILLIS_PER_MINUTE;
	
	/**
	 * Milliseconds in a standard day.
	 */
	public static final long MILLIS_PER_DAY = 24 * MILLIS_PER_HOUR;

	
	public static final String DATE_FORMAT_YYYY_MM_DD = "yyyy-MM-dd";
	
	public static final String DATE_FORMAT_YYYYMMDD = "yyyyMMdd";
	
	public static final String DATE_FORMAT_YYYY_MM = "yyyy-MM";

	public static final String DATE_FORMAT_YYYYMM = "yyyyMM";

	public static final String DATE_FORMAT_MM_DD = "MM-dd";
	
	public static final String DATE_FORMAT_YYYY_MM_DD_CN = "yyyy年MM月dd日";
	
	public static final String DATE_FORMAT_YYYY_MM_CN = "yyyy年MM月";
	
	public static final String DATE_FORMAT_MM_DD_CN = "MM月dd日";
	
	public static final String TIME_FORMAT_HH_MM_SS = "HH:mm:ss";
	
	public static final String TIME_FORMAT_HH_MM = "HH:mm";
	
	public static final String TIME_FORMAT_HH_MM_SS_CN = "HH点mm分ss秒";
	
	public static final String TIME_FORMAT_HH_MM_CN = "HH点mm分";
	
	public static Date millis2Date(long millis) {
		return new Date(millis);
	}
	
	public static String millis2String(long millis, String format){
		return date2String(millis2Date(millis), format);
	}

	public static Date string2Date(String str, String format) {
		SimpleDateFormat sdf = new SimpleDateFormat(format, Locale.getDefault());
		Date date = null;
		try {
			date = sdf.parse(str);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return date;
	}
	
	public static long string2Millis(String str, String format){
		if(TextUtils.isEmpty(str))
			return 0;
		Date date = string2Date(str, format);
		return date2Millis(date);
	}
	
	public static String date2String(Date date, String format){
		SimpleDateFormat dateformat = new SimpleDateFormat(format, Locale.getDefault());
		return dateformat.format(date);
	}
	
	public static long date2Millis(Date date){
		if(date == null)
			return 0;
		return date.getTime();
	}
	
	public static int getDay(Date date){
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		return c.get(Calendar.DAY_OF_MONTH);
	}
	
	public static int getMonth(Date date){
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		return c.get(Calendar.MONTH);
	}
	
	public static int getYear(Date date){
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		return c.get(Calendar.YEAR);
	}

	/**
	 * 根据当前时间显示相对时间，算出是今天、昨天、两天前、还是一周前
	 * 
	 * @param millis
	 * @return
	 */
	public static String getRelativeDate(long millis){
		long currMillis = System.currentTimeMillis();
		int currDaySum = (int) (currMillis / MILLIS_PER_DAY);
		int destDaySum = (int) (millis / MILLIS_PER_DAY);
		int dayDiff = currDaySum - destDaySum;
		if(dayDiff == 0){
			return millis2String(millis, TIME_FORMAT_HH_MM);
		}else if(dayDiff == 1){
			return "昨天" + millis2String(millis, TIME_FORMAT_HH_MM);
		}else if(dayDiff > 1 && dayDiff < 7){
			return "两天前";
		}else if(dayDiff > 6){
			return millis2String(millis, DATE_FORMAT_YYYY_MM_DD + " " + TIME_FORMAT_HH_MM_SS);
		}
		return null;
	}
	
    /**
     * 判断用户的设备时区是否为东八区（中国） 2014年7月31日
     * 
     * @return
     */
    public static boolean isChineseTimeZone() {
        boolean defaultVaule = true;
        if (TimeZone.getDefault() == TimeZone.getTimeZone("GMT+08"))
            defaultVaule = true;
        else
            defaultVaule = false;
        return defaultVaule;
    }
    
    /**
     * 根据不同时区，转换时间 2014年7月31日
     */
    public static Date transformTime(Date date, TimeZone oldZone,
            TimeZone newZone) {
        Date finalDate = null;
        if (date != null) {
            int timeOffset = oldZone.getOffset(date.getTime()) - newZone.getOffset(date.getTime());
            finalDate = new Date(date.getTime() - timeOffset);
        }
        return finalDate;
    }
    
    /**
     * 是否在时间范围内
     * 
     * @param startDate 开始时间，可以为null
     * @param endDate 结束时间，可以为null
     * @return 当开始时间和结束时间都为null，返回true
     */
    public static boolean isInRange(Date startDate, Date endDate){
		long current = System.currentTimeMillis();
		if (startDate != null && date2Millis(startDate) > current) {
			return false;
		} 
		if (endDate != null && date2Millis(endDate) < current) {
			return false;
		}
		return true;
	}

	public static int getDayOfWeekIndex(long millis){
		Calendar cal = Calendar.getInstance();
		cal.setTimeInMillis(millis);
		return cal.get(Calendar.DAY_OF_WEEK) - 1;
	}

	public static String getDayOfWeek(long millis) {
		String[] weekDays = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};
		int w = getDayOfWeekIndex(millis);
		if (w < 0)
			w = 0;
		return weekDays[w];
	}

	public static String getDayOfWeek(int dayOfWeek){
		String[] weekDays = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};
		return weekDays[dayOfWeek];
	}

	public static String getHommizationDate(String srcTime, String format) {
        long srcMillis = string2Millis(srcTime, format);
        long curMillis = System.currentTimeMillis();
        Calendar srcCal = Calendar.getInstance();
        srcCal.setTimeInMillis(srcMillis);
        Calendar curCal = Calendar.getInstance();
        curCal.setTimeInMillis(curMillis);
        long difference = curMillis - srcMillis;
        if(difference < MILLIS_PER_HOUR){ //1小时内
            return difference / MILLIS_PER_MINUTE + "分钟前";
        }else if(difference < MILLIS_PER_DAY * 2){ //2天内
            String hms = millis2String(srcMillis, TIME_FORMAT_HH_MM_SS);
            if(srcCal.get(Calendar.DAY_OF_MONTH) == curCal.get(Calendar.DAY_OF_MONTH)){
                return "今天 " + hms;
            }else{
                return "昨天 " + hms;
            }
        }else{
            return srcTime;
        }
	}
}
